﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sbook.AdminIstrator
{
    public partial class Banner : System.Web.UI.Page
    {
        private static string Id = "";
        private static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Data.Mysession.isLogin)
                {
                    BindGidBanner();
                    LoadDDLType();
                }
                else
                    Response.Redirect("/AdminIstrator/Login.aspx");
            }
        }

        //
        protected void BindGidBanner()
        {
            grBanner.DataSource = Business.BannerService.Banner_GetByAll();
            grBanner.DataBind();

            for (int i = 0; i < grBanner.Items.Count; i++)
            {
                switch (grBanner.Items[i].Cells[2].Text)
                {
                    case "1":
                        grBanner.Items[i].Cells[2].Text = "Hiển thị Slide trang chủ";
                        break;
                    case "2":
                        grBanner.Items[i].Cells[2].Text = "Hiển thị Quảng cáo 1";
                        break;
                    case "3":
                        grBanner.Items[i].Cells[2].Text = "Hiển thị Quảng cáo 2";
                        break;
                    case "4":
                        grBanner.Items[i].Cells[2].Text = "Hiển thị Quảng cáo 4";
                        break;

                }
                switch (grBanner.Items[i].Cells[3].Text)
                {
                    case "0":
                        grBanner.Items[i].Cells[3].Text = "Ẩn";
                        ((Button)grBanner.Items[i].FindControl("btnActive")).Text = "Hiện";
                        break;
                    case "1":
                        grBanner.Items[i].Cells[3].Text = "Hiện thị";
                        ((Button)grBanner.Items[i].FindControl("btnActive")).Text = "Ẩn";
                        break;
                }

                //if (((Button)grBanner.Items[i].FindControl("btnActive")).Text == "Hiển thị")
                //    ((Button)grBanner.Items[i].FindControl("btnActive")).Text = "Ẩn";
                //else
                //    ((Button)grBanner.Items[i].FindControl("btnActive")).Text = "Hiện";
            }

        }

        //Load dropdown
        private void LoadDDLType()
        {
            ddlType.Items.Clear();
            ddlType.Items.Add(new ListItem("----Nơi hiển thị----","0"));
            
            for (int i = 1; i <= 4; i++)
            {
                switch (i)
                {
                    case 1:
                        ddlType.Items.Add(new ListItem("Hiển thị Slide trang chủ", i.ToString()));
                        break;
                    case 2:
                        ddlType.Items.Add(new ListItem("Hiển thị quảng cáo 1", i.ToString()));
                        break;
                    case 3:
                        ddlType.Items.Add(new ListItem("Hiển thị quảng cáo 2", i.ToString()));
                        break;
                    case 4:
                        ddlType.Items.Add(new ListItem("Hiển thị quảng cáo 3", i.ToString()));
                        break;
                }
            }

            ddlType.DataBind();
        }
        //ITemcommand
        protected void grdBanner_ItemCommand(object sender, DataGridCommandEventArgs e)
        {
            string strCA = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Edit":
                    Id = strCA;
                    List<Data.BannerInfo> listU = Business.BannerService.Banner_GetById(Id);
                    BImgEdit.ImageUrl = listU[0].BLink;
                    ddlType.Text = listU[0].BType;
                    lblFile.Text = listU[0].BLink;
                    chkBActive.Checked = listU[0].BStatus == "1" ? true : false;
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    Insert = false;
                    break;
                default:
                    break;
              
            }
            
        }
        //Delete 
        protected void Delete_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            string Id = btn.CommandArgument.ToString();
            Business.BannerService.Banner_Delete(Id);
            BindGidBanner();
        }
        //Active
        protected void Active_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            string Id = btn.CommandArgument.ToString().Split(':')[0];
            string ActiveOld = btn.CommandArgument.ToString().Split(':')[1];
            string sql = "update Banner set BStatus=";
            if (ActiveOld == "0")
                sql += "1";
            else
                sql += "0";
            sql += " where BId=" + Id;
            Data.SqlDataProvider sp = new Data.SqlDataProvider();
            sp.ExecuteNonQuery(sql);
            BindGidBanner();
            
        }
        //Add click
        protected void Add_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            pnView.Visible = false;
            Insert = true;
            LoadDDLType();
            ddlType.SelectedValue = "0";
        }
        //SAveFile

        private string Save(FileUpload file)
        {
            string math = "";
            if (file.FileName != "")
            {
                math = Server.MapPath("/Upload/" + file.FileName);
                file.SaveAs(math);
                return "/Upload/" + file.FileName.ToString();
            }
            else
                return "";
        }
        //Save
        protected void Save_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Data.BannerInfo obj = new Data.BannerInfo();
                obj.BId = Id;
               
                obj.BStatus = chkBActive.Checked ? "1" : "0";
                obj.BType = ddlType.SelectedValue;
                if (Insert)
                {
                    if (obj.BType == "2" || obj.BType == "3" || obj.BType == "4")
                    {
                        Business.BannerService.Banner_Delete(Business.BannerService.Banner_GetByAll().FindAll(b => b.BType == obj.BType)[0].BId);
                    }
                    obj.BLink = this.Save(fileBImg);
                    Business.BannerService.Banner_Insert(obj);
                }
                else
                {
                    if (this.Save(fileBImg) != "")
                        obj.BLink = this.Save(fileBImg);
                    else
                        obj.BLink = lblFile.Text;
                    Business.BannerService.Banner_Update(obj);
                }
                BindGidBanner();
                pnUpdate.Visible = false;
                pnView.Visible = true;
            }
        }
        //Reset

        //Back
        protected void Back_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnView.Visible = true;
            BindGidBanner();
        }
    }
}